﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Settings;
using System.Threading;

namespace WindowsFormProject
{
    public partial class LoginPanel : Form
    {
        public LoginPanel()
        {
            InitializeComponent();
        }
        public static string UserID { get; set; }
        public static string[] UserRoles { get; set; }

        TeknoromaServis.TeknoromaWebServiceClassSoapClient ts;
        TeknoromaServis.KimlikBilgisi km;
        private void btnSignIn_Click(object sender, EventArgs e)
        {
            SignInMethod();
        }

        private void SignInMethod()
        {
            #region WebServis Ayarlar
            ts = new TeknoromaServis.TeknoromaWebServiceClassSoapClient();
            Settings.ServiceSettings.UserInfo ui = Settings.ServiceSettings.GetSettings();
            km = new TeknoromaServis.KimlikBilgisi()
            {
                User = ui.User,
                Password = ui.Password,
                SecurtyCode = ui.SecurtyCode
            };
            #endregion
            this.Height = 200;
            ts.LoginValidationCompleted += new EventHandler<TeknoromaServis.LoginValidationCompletedEventArgs>(ts_LoginValidationCompleted);
            ts.LoginValidationAsync(km, txtUserName.Text, txtPassword.Text);
            lblMessage.Text = "Sign In.. Please Waiting....";
            pictureBox1.Visible = true;
            btnSignIn.Enabled = false;
        }

        void ts_LoginValidationCompleted(object sender, TeknoromaServis.LoginValidationCompletedEventArgs e)
        {
            try
            {
                UserID = e.Result;

                if (UserID != null)
                {
                    lblMessage.Text = "Sign In Successful.. Roles Are Taken... Please Waiting....";
                  
                    ts.GetUserRolesCompleted += new EventHandler<TeknoromaServis.GetUserRolesCompletedEventArgs>(ts_GetUserRolesCompleted);
                    ts.GetUserRolesAsync(km, txtUserName.Text);

                }
                else
                {
                    this.Height = 150;
                    lblMessage.Text = "";
                    pictureBox1.Visible = false;
                    btnSignIn.Enabled = true;
                    MessageBox.Show("Invalid User Name or Password");

                }
            }
            catch
            {
                this.Height = 170;
                lblMessage.Text = "Not Connected To The Server";
                btnSignIn.Enabled = true;
            }
        }

        void ts_GetUserRolesCompleted(object sender, TeknoromaServis.GetUserRolesCompletedEventArgs e)
        {
            try
            {
                UserRoles = e.Result;
                this.Hide();
                MainForm mf = new MainForm();
                mf.Show();
            }
            catch
            {
                this.Height = 170;
                lblMessage.Text = "Server Connection Lost... Please Try Later..";
                UserID = null;
              
                btnSignIn.Enabled = true;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                SignInMethod();
            }
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                txtPassword.Focus();
            }
        }


    }
}
